<?php
/**
 * @annotate 后台用户列表。添加。修改。删除
 * @group   Admin/Haiyin
 * @author   mengke.yue <mengke.yue@gmail.com>
 */
class UserAction extends BaseAction {
	
	public function adduser(){
		$this->assign('Title',"人才来了添加用户");
		$url = U('User/user_list');
   	    $this->checkPost();
        $Admin = D('User');
		$data['uname'] = getRequest('validation_name');
        $pwd = getRequest('validation_password');
        $data['login_salt'] = rand(10000,99999);
		$data['pwd'] = md5(md5($pwd).$data['login_salt']);
        $data['group'] = getRequest('validation_select');
        $data['ctime'] = time();
        $result=$Admin->add_user($data);
        if ($result) {
			$this->assign("jumpUrl",$url);//跳转
        	$this->success("添加成功！");
		}
	}

	public function user_list(){
		$this->assign('Title',"用户列表");
		$list = D('admin')->where(array('is_del'=>0))->order('ctime desc')->findPage(20);
		$this->assign('list',$list);
		$this->display();
	}
	public function del_user(){
		$id = getRequest('id');
		$da = D('User')->del_user($id);
		if($da){
			$this->ajaxReturn('','success',1);
		}else{
			$this->ajaxReturn('','error',0);
		}
	}
	public function edit_user(){
		$this->assign('Title',"人才来了编辑用户");
		$url = U('User/user_list');
		$aid = getRequest('aid');
		$Admin = D('User');
		$data = $Admin->get_user($aid);
		$this->assign('data',$data);
   	    $this->checkPost();
        
		$data['uname'] = getRequest('validation_name');
        $pwd = getRequest('validation_password');
        if($pwd !=""){
        	$data['login_salt'] = rand(10000,99999);
			$data['pwd'] = md5(md5($pwd).$data['login_salt']);
        }
        
        $data['group'] = getRequest('validation_select');
        $aaid = getRequest('aaid');
        $result=$Admin->edit_user($aaid,$data);
        
        if ($result==true) {
			$this->assign("jumpUrl",$url);//跳转
			$this->success("更新成功！");
        	
		}
	}

	public function check_pword(){
		$admin = $_SESSION['admin'];
		$pwd = $_POST['pwd'];
		$pwd1 = md5(md5($pwd).$admin['login_salt']);
		if($pwd1 == $admin['pwd']){
			$this->ajaxReturn('','success',1);
		}else{
			$this->ajaxReturn('','error',0);
		}
	}
	public function edit_password(){
		$this->assign('Title',"修改密码");
		$admin = $_SESSION['admin'];
		$this->assign('admin',$admin);
		$this->checkPost();
		$pwd = getRequest('pwd2');
		$da = D('User')->edit_pwd($admin['aid'],$pwd);
		$url = U('Home/index');
		if($da){
			$this->assign("jumpUrl",$url);//跳转
			$this->success("修改成功！");      	
		}
	}

	public function agent(){
		$agent = D('User')->getagentlist();
		$this->assign('agent',$agent);
		$this->display();
	}
	public function edit_agent(){
		$pro = getarea();
		$agent = D('User')->getagent($_GET['aid']);
		$this->assign('agent',$agent);
		$this->assign('pro',$pro);
		$this->display();
	}
	public function doedit_agent(){
		$url = U('User/agent');
		$result = D('User')->editagent($_POST);
		if($result){
			$this->assign("jumpUrl",$url);//跳转
			$this->success("修改成功！");      	
		}
	}
	public function del_agent(){
		$aid = $_POST['aid'];
		$map['is_del'] = 1;
		$da = D('agent')->where(array('aid'=>$aid))->save($map);
		if($da){
			$this->success("删除成功！",'__URL__/agent');    
		}
	}
	public function getarea(){
		$type = $_POST['type'];
		$aid = $_POST['aid'];
		$area  = getarea($type,$aid);
		exit(json_encode($area));
	}
}
?>